package com.bjqg.bjqg01.bjqg.utils;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

/**
 * Created by Administrator on 2018/1/10.
 */

public class WebService {

    private static WebService webService;

    public static WebService getInstance() {
        if (webService == null) {
            synchronized (WebService.class) {
                if (webService == null) {
                    webService = new WebService();
                }
            }
        }
        return webService;
    }

    private String result;

    public String getWebService(String WSDL_URI, String namespace, String methodName) {
        SoapObject request = new SoapObject(namespace, methodName);
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId
//        request.addProperty("op", "getAllStudentInfo2");
//        request.addProperty("userId", "");
        //创建SoapSerializationEnvelope 对象，同时指定soap版本号(之前在wsdl中看到的)
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER12);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = true;//由于是.net开发的webservice，所以这里要设置为true

        HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
        try {
            httpTransportSE.call(null, envelope);//调用
        } catch (IOException | XmlPullParserException e) {
            e.printStackTrace();
        }

        // 获取返回的数据
        SoapObject object = (SoapObject) envelope.bodyIn;
        // 获取返回的结果
        if (object==null){
            result=null;
        }else {
            result = object.getProperty(0).toString();
        }
//        Log.d("debug", result);
        return result;
    }
}
